package com.dldata.devtools30.controller.si;

import com.dldata.devtools30.entity.DevtFormListtemplateEntity;
import com.dldata.devtools30.entity.DevtFormMetadataEntity;
import com.dldata.devtools30.repository.metadata.DevtModelMetadataTableDao;
import com.dldata.devtools30.repository.sysform.DevtFormDesignEntityDao;
import com.dldata.devtools30.repository.sysform.DevtFormListtemplateEntityDao;
import com.dldata.devtools30.repository.sysform.DevtFormMetadataDao;
import com.dldata.devtools30.utils.StringUtil;
import com.google.common.collect.Lists;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;
import java.util.Map;
/**

 */

@Controller
@RequestMapping(value = {"/V_DevtFormDataListController","/FormApiList30"})
public class V_DevtFormDataListController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    DevtFormDesignEntityDao formDesignDao;
    @Autowired
    DevtFormMetadataDao sysCrfFormMetadataDao;
    @Autowired
    DevtModelMetadataTableDao metadataTableDao;
    @Autowired
    DevtFormListtemplateEntityDao sysCrfFormListtemplateEntityDao;

    @RequestMapping(value = {"/toDataList",""})
    public void toDataList(@RequestParam(required = false)String formId, @RequestParam(required = false)final String dataId
            , @RequestParam(required = false)String mobanId, @RequestParam(required = false) String formStatus,
                             @RequestParam(required = false)String mDataId, Model model,
                             @RequestParam Map<String, Object> param, HttpServletRequest request, HttpServletResponse response)throws Exception{

        DevtFormListtemplateEntity sysCrfFormListtemplateEntity =null;
        if(!StringUtil.isEmptyOrLength0(mobanId)){
            sysCrfFormListtemplateEntity = sysCrfFormListtemplateEntityDao.findById(mobanId).get();
        }
        if(sysCrfFormListtemplateEntity==null&&!StringUtil.isEmptyOrLength0(formId)){
            DevtFormMetadataEntity sysCrfFormMetadataEntity1 = Lists.newArrayList(sysCrfFormMetadataDao.findAll()).stream().filter(
                    sysCrfFormMetadataEntity -> {
                        return sysCrfFormMetadataEntity.getTableid().equalsIgnoreCase(formId) && sysCrfFormMetadataEntity.getTabletype().equalsIgnoreCase("父表");
                    }
            ).findFirst().orElse(null);
            if(sysCrfFormMetadataEntity1!=null){
                sysCrfFormListtemplateEntity = sysCrfFormListtemplateEntityDao.findById(sysCrfFormMetadataEntity1.getTempleid()).get();
            }
        }
        if(sysCrfFormListtemplateEntity==null){
            try {
                response.getWriter().println("Templeid is null.");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }
        String templateid=sysCrfFormListtemplateEntity.getId();
        String dispatcherUrl="/V_SysFromPreviewController/toDldsnformPreview?1=1" ;
        dispatcherUrl=dispatcherUrl+"&tempid="+templateid;
        try {
            request.getRequestDispatcher(dispatcherUrl).forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
